package com.ly.base.chapter04.asynchronous;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

public class AAExecutorDemo {
        static final int INDEX = 0;
        static final int TIME = 1;
        static final int ORDER_ID = 2;
        static final int PRODUCT_ID = 3;
        static final int CATEGORY_ID = 4;
        static final int CATEGORY_CODE = 5;
        static final int BRAND = 6;
        static final int PRICE = 7;
        static final int USER_ID = 8;
        static final int USER_AGE = 9;
        static final int USER_SEX = 10;
        static final int USER_REGION = 11;
        static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

        static Logger logger = LoggerFactory.getLogger("Test");
        static final String DATA_URL = "D:\\heimaWenJian\\学习笔记\\函数式编程（Stream API）\\Funcation\\LearningBase\\data.txt";

        //1、显示显示了线程池
        //2、函数对象嵌套使用，可读性差
        public static void main(String[] args) {
                logger.info("开始统计");
                /**
                 * 将处理结果的逻辑放在 mothlySalesReport 之外
                 * 做法1、将结果作为方法的返回值返回
                 * 做法2：将处理结果的逻辑作为函数对象传递给方法(未做)
                 */
                new Thread(() -> {
                        Map<YearMonth, Long> map = monthlySalesReport();
                        for (Map.Entry<YearMonth, Long> e : map.entrySet()) {
                                logger.info(e.toString());
                        }
                }).start(); // 异步操作
//                monthlySalesReport(); // 同步操作
                logger.info("执行其它操作");

//                // 线程池方式
//                try {
//                       ExecutorService service = Executors.newFixedThreadPool(3);
//                       logger.info("开始统计");
//                       service.submit(AAExecutorDemo::monthlySalesReport);
//                       logger.info("执行其它操作");
//                } catch (Exception e) {
//                        throw new RuntimeException(e);
//                }



        }

        private static Map<YearMonth, Long> monthlySalesReport() {
                try (Stream<String> lines = Files.lines(Path.of(DATA_URL))) {
                        Map<YearMonth, Long> collect = lines.skip(1)
                                .map(line -> line.split(","))
                                .collect(groupingBy(array -> YearMonth.from(formatter.parse(array[TIME])), TreeMap::new, counting()));
                     /*   for (Map.Entry<YearMonth, Long> e : collect.entrySet()) {
                                logger.info(e.toString());
                        }*/
                        return collect;
                } catch (IOException e) {
                        throw new RuntimeException(e);
                }
        }

}
